package br.com.quad.dao;

import java.util.List;

import br.com.quad.bean.TipoServico;


public class TipoServicoDAOHibernate implements TipoServicoDAO {
	
	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<TipoServico> hibernateDAO = null;
	
	public TipoServicoDAOHibernate(){
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getTipoServicoDAO();
	}

	@Override
	public boolean inserir(TipoServico tipoServico) throws Exception {
		boolean ok = false;
		
		try {
			hibernateDAO.add(tipoServico);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@Override
	public boolean alterar(TipoServico tipoServico) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.update(tipoServico);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean excluir(TipoServico tipoServico) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.remove(tipoServico);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;

	}
	
	@Override
	public boolean isExclusaoLiberada(TipoServico tipoServico) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TipoServico> getSelecionar(String descricao) {
		String 	sql = "FROM TipoServico WHERE codigoTipoServico <> 0";

		if (!(descricao.isEmpty())) {
			sql += " AND descricao like '" + descricao + "%' ";  
		}	

		sql += " ORDER BY descricao";		
 
		List<TipoServico> lista = hibernateDAO.getSession().createQuery(sql).list();

		return lista;
	}
	@Override
	public TipoServico getTipoServico(int codigoTipoServico) {
		return hibernateDAO.get(codigoTipoServico);
	}

	@Override
	public List<TipoServico> getListarTodos() {
		return hibernateDAO.listAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExiste(TipoServico tipoServico){
		String 	sql = 	" FROM TipoServico " +
						" WHERE codigoTipoServico <> "+tipoServico.getCodigoTipoServico()+""+
						" AND descricao like '" + tipoServico.getDescricao() + "%' ";

		List<TipoServico> lista = hibernateDAO.getSession().createQuery(sql).list();
	    
		if (lista.size()<1) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void inicialize(TipoServico tipoServico) {
		// TODO Auto-generated method stub

	}

}
